﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;
using HYElectricKernel;

namespace HYRElectric.LightningCalculation
{
	public partial class AddEditCityThunderstormDaysForm : System.Windows.Forms.Form
	{
		public string NewCityName
		{
			get
			{
				return this.tbCityName.Text;
			}
		}

		public double NewCount
		{
			get
			{
				return Convert.ToDouble(this.tbCount.Text);
			}
		}

		public AddEditCityThunderstormDaysForm(string currentCityName, double currentCount, List<string> citySNameList)
		{
			if (currentCityName.CompareTo("") == 0)
			{
				this.m_IsAdd = true;
			}
			else
			{
				this.m_IsAdd = false;
			}
			this.m_CurrentCityName = currentCityName;
			this.m_CurrentCount = currentCount;
			this.m_CitySNameList = citySNameList;
			this.InitializeComponent();
		}

		private void AddEditCityThunderstormDaysForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			if (this.m_IsAdd)
			{
				this.Text = "添加城市年雷暴日信息";
			}
			else
			{
				this.Text = "编辑城市年雷暴日信息";
				this.tbCityName.Text = this.m_CurrentCityName;
				this.tbCount.Text = this.m_CurrentCount.ToString();
			}
			this.LoadDlgData();
		}

		private void AddEditCityThunderstormDaysForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.Cancel)
			{
				return;
			}
			string cityName = this.tbCityName.Text;
			if (this.m_IsAdd)
			{
				if (this.m_CitySNameList.FindIndex((string name) => name.CompareTo(cityName) == 0) != -1)
				{
					MessageBox.Show("新添加的地名已经存在", "盈建科提示");
					e.Cancel = true;
				}
			}
			else if (this.m_CitySNameList.FindIndex((string name) => name.CompareTo(cityName) == 0) != -1 && cityName.CompareTo(this.m_CurrentCityName) != 0)
			{
				MessageBox.Show("修改的地名已经存在", "盈建科提示");
				e.Cancel = true;
			}
			string text = this.tbCount.Text;
			double num = 0.0;
			if (!this.ConvetToDouble(text, ref num))
			{
				MessageBox.Show("雷暴日数不是正数", "盈建科提示");
				e.Cancel = true;
			}
			this.SaveDlgData();
		}

		private void SaveDlgData()
		{
			string workingLocation = Product.WorkingLocation;
			string xmlPath = Path.Combine(workingLocation, "AddEditCityThunderstormDaysForm.xml");
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(workingLocation, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			xmlHelper.SetValue("xPos", base.Location.X.ToString());
			xmlHelper.SetValue("yPos", base.Location.Y.ToString());
			xmlHelper.SaveXml(xmlPath);
		}

		private void LoadDlgData()
		{
			string text = Path.Combine(Product.WorkingLocation, "AddEditCityThunderstormDaysForm.xml");
			if (!File.Exists(text))
			{
				return;
			}
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(text, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			int x = Convert.ToInt32(xmlHelper.GetValue("xPos"));
			int y = Convert.ToInt32(xmlHelper.GetValue("yPos"));
			base.Location = new System.Drawing.Point(x, y);
		}

		private bool ConvetToDouble(string strValue, ref double dValue)
		{
			bool result;
			try
			{
				dValue = Convert.ToDouble(strValue);
				if (Geometry.LessThan(dValue, 0.0))
				{
					result = false;
				}
				else
				{
					result = true;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private bool m_IsAdd;

		private List<string> m_CitySNameList = new List<string>();

		private string m_CurrentCityName = "";

		private double m_CurrentCount;
	}
}
